//turret.txt - Simple script for turrets
//Cell 0,1 - Stuff done flag. If both 0, nothing. Otherwise If non-zero, this 
//turret wont attack. If the turret is itself attacked, will set the not-attack setting back to 0.
//Cell 2,3 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

short i,target;
short last_spawn;

short spawn_count = 0;
body;

beginstate INIT_STATE;
	last_spawn = get_current_tick();
	if (gf(0,1) > 0)
		erase_char(ME);	break;

beginstate DEAD_STATE;
	if ((get_memory_cell(2) != 0) || (get_memory_cell(3) != 0))
		set_flag(get_memory_cell(2),get_memory_cell(3),1);
break;

beginstate START_STATE; 
	if (who_shot_me() >= 0) {


		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

		
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (gf(0,1) > 0)
		end();
		
	if (target_ok() == FALSE)
		set_state(START_STATE);
	if (dist_to_char(get_target()) > 8) {
		set_foe_target(ME,-1);
		set_state(START_STATE);
		}

	if ((target_ok()) || (who_shot_me() >= 0)) {
		if ((tick_difference(last_spawn,get_current_tick()) > 1) && (get_attitude(ME) >= 10)) {
			if ((51 + spawn_count <= 58) && (char_ok(51 + spawn_count))) {
				set_attitude(51 + spawn_count,10);
				place_particle_num(51 + spawn_count,21,7,10);
				print_str_color("The vlish takes control of a nearby creation!",2);
				run_char_animation(5,1,50);	
				pc_heard_sound_delay(136,100);						
				last_spawn = get_current_tick();
				spawn_count = spawn_count + 1;
					
				end();
				}
		
			spawn_count = spawn_count + 1;

			}
		}

	do_attack();

	turret_heal();
break;

beginstate TALKING_STATE;
	print_str("All it really is is a big fungus. It can't communicate at all.");
break;